<?php
class Advert extends CS_Controller 
{
    private $position = [
        '1' => '首页',
    ];
    private $opentype = [
        'link' => '链接',
    ];
    private $status = [
        '0' => '禁用',
        '1' => '启用',
    ];
    public function _init() 
    {
        $this->load->library('pagination');
        $this->load->model('advert_model','advert');
    }
    
    public function grid($pg = 1)
    {
        $page_num = 15;
        $num = ($pg - 1) * $page_num;
        $getData = $this->input->get();
        $getData['provider_id'] = $this->uid;
        $config['first_url'] = site_url('advert/grid').$this->pageGetParam($this->input->get());
        $config['base_url'] = site_url('advert/grid');
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['total_rows'] = $this->advert->findByCount($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->advert->findPageList($num,$page_num,$getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $data['position'] = $this->position;
        $data['status'] = $this->status;
        $data['opentype'] = $this->opentype;
        $this->load->view('advert/grid', $data);
    }
    
    public function add()
    {
        $data['position'] = $this->position;
        $data['opentype'] = $this->opentype;
        $this->load->view('advert/add',$data);
    }
    
    public function addPost()
    {
        $param['title'] = trim($this->input->post('title',true));
        $param['opentype'] = trim($this->input->post('opentype',true));
        $param['provider_id'] = $this->uid;
        $param['openvar'] = $this->input->post('openvar',true);
        $param['picture'] = $this->input->post('picture',true);
        $param['sort'] = $this->input->post('sort',true);
        $param['status'] = $this->input->post('status',true);
        $param['create_time'] = date('Y-m-d H:i:s');
        $param['update_time'] = date('Y-m-d H:i:s');
        if ( !empty($this->input->post('position')) ){
            $param['position'] = $this->input->post('position');
        }
        $error = $this->validateAdd($param);
        if ( !empty($error) ){
            $this->error('advert/grid', '', $error);
        }
        $status = $this->advert->insert($param);
        if( $status ){
            $this->success('advert/grid', '', '添加成功');
        } else{
            $this->error('advert/add', '', '添加失败');
        }
    }
    
    public function edit($id)
    {
        $res = $this->advert->findOne(['id' => $id,'provider_id' => $this->uid]);
        if ( $res->num_rows() <= 0 ){
            $this->error('advert/grid', '', '该广告不存在');
        }
        $data['advert'] = $res->row();
        $data['position'] = $this->position;
        $data['opentype'] = $this->opentype;
        $this->load->view('advert/edit',$data);
    }
    
    public function editPost()
    {
        $id = $this->input->post('id');
        $param['title'] = trim($this->input->post('title',true));
        $param['opentype'] = trim($this->input->post('opentype',true));
        $param['openvar'] = $this->input->post('openvar',true);
        $param['picture'] = $this->input->post('picture',true);
        $param['status'] = $this->input->post('status',true);
        $param['sort'] = $this->input->post('sort',true);
        $param['update_time'] = date('Y-m-d H:i:s');
        if ( !empty($this->input->post('position')) ){
            $param['position'] = $this->input->post('position');
        }
        $error = $this->validateAdd($param);
        if ( !empty($error) ){
            $this->error('advert/grid', '', $error);
        }
        $status = $this->advert->update($param,['id' => $id,'provider_id' => $this->uid]);
        if( $status ){
            $this->success('advert/grid', '', '修改成功');
        } else{
            $this->error('advert/add', '', '修改失败');
        }
    }
    
    public function delete($id)
    {
        $res = $this->advert->findOne(['id' => $id,'provider_id' => $this->uid]);
        if ( $res->num_rows() <= 0 ){
            $this->error('advert/grid', '', '该广告不存在');
        }
        $status = $this->advert->delete($id);
        if( $status ){
            $this->success('advert/grid', '', '删除成功');
        } else{
            $this->error('advert/add', '', '删除失败');
        }
    }
    /**
     * 验证
     * @param unknown $param
     */
    public function validateAdd($param){
        
        $error = array();
        if( $this->validateParam($param['title']) ){
            $error['title'] = '标题不能为空';
        }
        if( $this->validateParam($param['opentype']) ){
            $error['opentype'] = '跳转类型不能为空';
        }
        if( $this->validateParam($param['openvar']) ){
            $error['openvar'] = '打开参数不能为空';
        }
        if( $this->validateParam($param['picture']) ){
            $error['picture'] = '图片不能为空';
        }
        if( $this->validateParam($param['sort']) ){
            $error['sort'] = '排序不能为空';
        }
        if( !is_numeric($param['sort']) ){
            $error['sort_number'] = '排序必须为数字';
        }
        return $error;
    }
}